#!/usr/bin/env python
#coding=utf-8
"""
sample ftp server
作业：
- 完成put
- 查看文件列表
- 完整性验证
- 内存读取优化
- 多用户
"""
import socket

host, port = '127.0.0.1', 50010
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((host, port))
fn = raw_input("input your filename: ").strip()
s.sendall('get %s' % fn)
buf = ''
len_data = 0

while True:
	tmp = s.recv(1024)
	if not tmp: break
	if not len_data:
		str_len, str_tmp = tmp.split('\t', 1)
		len_data = int(str_len)
		buf += str_tmp
	else:
		buf += str_tmp
	print "recv ratio %s " % (100 * len(buf)/len_data) + '%'

ofh = open(fn, 'wb+')
ofh.write(buf)
ofh.close()

print 'congratulations, your files %s has been received successfully, file len%s' %(fn, len_data)